<template>
  <div class="xtx-pay-page">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/cart">购物车</XtxBreadItem>
        <XtxBreadItem >支付订单</XtxBreadItem>
      </XtxBread>
      <!-- 付款信息 -->
      <div class="pay-info" v-if="order">
        <i class="icon iconfont icon-queren2"></i>
        <div class="tip">
          <p>订单提交成功！请尽快完成支付</p>
          <p v-if="order.countdown>-1">支付还剩<span>{{timeText}}</span>，超时将取消订单</p>
          <p v-else>您的订单已经超时</p>
        </div>
        <div class="amount">
          <span>应付总额：</span>
          <span>￥{{order.payMoney}}</span>
        </div>
      </div>
      <!-- 付款方式 -->
      <div class="pay-type">
        <p class="head">选择以下支付方式付款</p>
        <div class="item">
          <p>支付平台</p>
          <a class="btn wx" href="javascript:;"></a>
          <a class="btn alipay" :href="payUrl" target="_blank" @click="visibleDialog=true"></a>
        </div>
        <div class="item">
          <p>支付方式</p>
          <a class="btn active" href="javascript:;">招商银行</a>
          <a class="btn" href="javascript:;">工商银行</a>
          <a class="btn" href="javascript:;">建设银行</a>
          <a class="btn" href="javascript:;">农业银行</a>
          <a class="btn" href="javascript:;">交通银行</a>
        </div>
      </div>
      <!-- 支付中的对话框 -->
      <XtxDialog title="正在支付..." v-model:visible="visibleDialog">
        <template #body>
          <div class="pay-wait">
            <img src="@/assets/images/load.gif">
            <div v-if="order" class="textBox">
              <p>如果支付成功：</p>
              <router-link :to="`/member/order/${order.id}`">查看订单详情></router-link>
              <p>如果支付失败：</p>
              <router-link to="/">查看相关疑问></router-link>
            </div>
          </div>
        </template>
      </XtxDialog>
    </div>
  </div>
</template>

<script>
import XtxBread from '@/components/library/xtx-bread.vue'
import XtxBreadItem from '@/components/library/xtx-bread-item.vue'
import { useRoute } from 'vue-router'
import { ref } from '@vue/reactivity'
import { findOrderDetail } from '@/api/order'
import { usePayTime } from '@/hooks'
import { baseURL } from '@/utils/request'
import XtxDialog from '@/components/library/xtx-dialog.vue'
export default {
  components: { XtxBread, XtxBreadItem, XtxDialog },
  setup () {
    // 根据地址栏id发起请求 拿数据
    const route = useRoute()
    const order = ref(null)
    findOrderDetail(route.query.orderId).then(data => {
      order.value = data.result
      // 后端提供countdown 倒计时秒数
      if (data.result.countdown > -1) {
        start(data.result.countdown)
      }
    })
    // 倒计时工具函数
    const { start, timeText } = usePayTime()
    // 支付地址
    // const payUrl = 基准地址+支付页面地址+订单id+回调地址
    const redirect = encodeURIComponent('http://localhost:8080/#/pay/callback')
    const payUrl = `${baseURL}pay/aliPay?orderId=${route.query.orderId}&redirect=${redirect}`
    // 控制正在支付对话框页面的展示
    const visibleDialog = ref(false)
    return {
      order,
      timeText,
      payUrl,
      visibleDialog
    }
  }
}
</script>

<style lang="less" scoped>
.xtx-pay-page{
  .container{
    .pay-info{
      width: 100%;
      height: 240px;
      background-color: #fff;
      padding: 0 80px;
      display: flex;
      align-items: center;
      .icon{
        color:@xtxColor;
        font-size: 80px;
      }
      .tip{
        padding-left: 10px;
        flex: 1;
        p{
          &:first-child{
            font-weight: normal;
            font-size: 20px;
            margin-bottom: 10px;
          }
          &:last-child{
            color: #999;
            font-size: 16px;
          }
        }
      }
      .amount{
        margin-left: 250px;
        span{
          &:first-child{
            color: #999;
          }
          &:last-child{
            color: @priceColor;
            font-size: 20px;
          }
        }
      }
    }
    .pay-type{
      width: 100%;
      background-color: #fff;
      height: 390px;
      margin-top: 20px;
      .head{
        padding-left: 30px;
        height: 70px;
        line-height: 69px;
        border-bottom: 1px solid #f5f5f5;
      }
      .item{
        p{
          margin-top: 10px;
          padding-left: 30px;
          line-height: 60px;
          height: 60px;
        }
        .btn{
          width: 150px;
          height: 50px;
          line-height: 48px;
          border: 1px solid #e4e4e4;
          text-align: center;
          margin-left: 30px;
          color: #666666;
          display: inline-block;
          &:hover,&.active{
            border-color: @xtxColor;
          }
        }
        .alipay{
          background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/7b6b02396368c9314528c0bbd85a2e06.png) no-repeat center / contain;
        }
        .wx{
          background: url(https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66f98cff8649bd5ba722c2e8067c6ca.jpg) no-repeat center / contain;
        }
      }
    }
  }
}
.pay-wait{
  display: flex;
  justify-content: space-around;
  p{
    margin-top: 30px;
    font-size: 14px;
  }
  a{
    color: @xtxColor;
  }
}
</style>
